/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.provider;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.xbdun.common.enums.PkGenerateEnum;
import com.xbdun.common.util.PkGenerator;

/**
 * 动态XbdunOrleDepartDo的SQL语句提供者
 * 部门角色中间表
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
public class OrleDepartSqlProvider {
	//查询部门关联下的角色
	private static final String QUERY_SQL = "SELECT a.id,a.orgCode,a.roleId,a.createTime,a.optionUser,a.blSpecial,b.roleName,b.roleShort FROM xbdun_orle_depart a,xbdun_role b WHERE a.orgCode =#{orgCode} AND a.code=#{departCode} AND a.roleId = b.id ORDER BY b.createTime LIMIT #{pageIndex},#{pageSize}";

	//日志
	Log log = LogFactory.getLog(OrleDepartSqlProvider.class);
	
	/**
	 * 动态sql更新对象
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateDynXbdunOrleDepartSql(Map<String,Object> map){
		
		StringBuilder sbSql = new StringBuilder();
		Map<String,Object> params = (Map<String, Object>) map.get("param");
		String _where = constWhere(map);
		boolean bl = false;
		
		Assert.isTrue(StringUtils.isNotEmpty(_where),"动态更新操作，条件Where不存在");
		
		//移除Where条件
		sbSql.append("UPDATE xbdun_orle_depart SET ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = params.entrySet().iterator();
			while(iparam.hasNext()){
				if(bl)sbSql.append(",");
				
				Entry<String, Object> enty = iparam.next();
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
			//设置Where条件
			if(!StringUtils.isEmpty(_where)){
				sbSql.append(" WHERE "+_where);
			}
		}else{
			//参数为空则，清空所有的SQL
			sbSql.setLength(0);
		}
		String sqlContent = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return sqlContent;
	}

	/**
	 * 判断是否存在,不为null则是存在
	 * @param map
	 */
	public String existSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		@SuppressWarnings("unchecked")
		Map<String,Object> mparam = (Map<String,Object>)params.get("param");
		boolean bl = false;
		sbSql.append("SELECT COUNT(*) FROM xbdun_orle_depart WHERE ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = mparam.entrySet().iterator();
			while(iparam.hasNext()){
				
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
		}
		
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
	
	//部门关联角色
	public String addOrleDepartsSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		List<Long> list = (List<Long>)params.get("list");
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("INSERT INTO xbdun_orle_depart(id,orgCode,roleId,code,createTime,optionUser,blSpecial) VALUES");
		boolean bl = false;
		for(Long longx : list){
			if(bl)sbSql.append(",");
			if(null!=longx&&longx.longValue()>0){
				sbSql.append("("+PkGenerator.generateLongPk(PkGenerateEnum.SYSFRAME)+",#{a.orgCode},"+longx+",#{a.code},now(),#{a.optionUser},#{a.blSpecial})");
				if(!bl)bl = !bl;
			}
		}
		String contentSql = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	//查询关联机构下的部门角色是否归属于部门
	public String chekRoleDepartOkSql(Map<String,Object> params){
		@SuppressWarnings("unchecked")
		Set<Long> set = (Set<Long>)params.get("roleDptIds");
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("SELECT COUNT(*) FROM xbdun_orle_depart WHERE orgCode=#{orgCode} AND code=#{departCode} AND id IN (");
		boolean bl = false;
		for(Long longx : set){
			if(bl)sbSql.append(",");
			sbSql.append(longx);
			if(!bl)bl = !bl;
		}
		sbSql.append(")");
		String contentSql = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	//查询根据部门关联查询到用户拥有的角色
	public String queryPagerSql(Map<String,Object> map){
		Long userCode = (Long)map.get("userCode");
		if(null==userCode || userCode.longValue()<=0)return QUERY_SQL;
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("SELECT ax.*,ISNULL(bx.orleDepartId) blUserRole FROM (");
		//部门关联所有角色
		sbSql.append(QUERY_SQL);
		sbSql.append(")ax LEFT JOIN (");
		//查询用户已经存在的角色
		sbSql.append("SELECT a.orleDepartId FROM xbdun_power_user_menu a WHERE a.userCode = #{userCode} AND a.orgCode = #{orgCode} AND a.code = #{departCode} GROUP BY a.orleDepartId");
		sbSql.append(")bx ON ax.id = bx.orleDepartId");
		String contentSql = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	//生成查询分页where条件
	private String constWhere(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//where查询条件
		@SuppressWarnings("unchecked")
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		if(!CollectionUtils.isEmpty(_where)){
			Iterator<Entry<String, Object>> iparam = _where.entrySet().iterator();
			boolean bl = false;
			while(iparam.hasNext()){
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				
				sbSql.append(enty.getKey()+" = "+"#{_where."+enty.getKey()+"}");
				if(!bl)bl = true;
			}
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
}
